/*
 * Copyright 2009 Gary Brown
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 *
 * Change History:
 * 18 Mar 2009 : Initial version created by gary
 */
package org.betonthemove.ui.android;

import org.betonthemove.platform.*;

import android.app.Activity;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.res.Configuration;
import android.os.Bundle;
import android.widget.*;

public class AccountFundsView extends Activity implements Runnable {

    private static final int ERROR_MESSAGE_DISPLAY_DELAY = 4000;
	private static final int WAITING_DIALOG = 1;
    private static final int ERROR_DIALOG = 2;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.accountfundsview);

        m_availableBalance = (TextView)findViewById(R.id.availableBalance);
        m_balance = (TextView)findViewById(R.id.balance);

        runOnUiThread(this);   
    }
		
	@Override
    protected Dialog onCreateDialog(int id) {
        switch (id) {
            case WAITING_DIALOG: {
                ProgressDialog dialog = new ProgressDialog(this);
                dialog.setMessage("Retrieving account funds...");
                dialog.setIndeterminate(true);
                dialog.setCancelable(true);
                return dialog;
            }
            case ERROR_DIALOG: {
                ProgressDialog dialog = new ProgressDialog(this);
                dialog.setMessage("ERROR: "+m_error);
                dialog.setIndeterminate(true);
                dialog.setCancelable(true);
                return dialog;
            }
        }
        return null;
    }

    public void run() {
    	    	
    	if (m_state == 0) {
    		showDialog(WAITING_DIALOG);
    		
    		m_state++;
    		
    		new Thread(this).start();
    	} else if (m_state == 1) {

    		BettingPlatform platform=BettingPlatformFactory.getBettingPlatform();
        
	        try {
		        m_accountFunds = platform.getAccountFunds();
		        
	    		m_currency = DisplayHelper.getCurrencySymbol(
	    					platform.getCurrency());
	    		
		        m_state++;
	        } catch(Exception e) {
	        	m_error = e.getLocalizedMessage();
	        	
	        	m_state = 5;
	        }
	        
	        runOnUiThread(this);

    	} else if (m_state == 2) {
    		
    		m_availableBalance.setText(m_currency+
    					m_accountFunds.getAvailableBalance());
    		m_balance.setText(m_currency+
						m_accountFunds.getBalance());
     		
    		m_state = 0;

    		dismissDialog(WAITING_DIALOG);
	        
    	} else if (m_state == 5) {

    		dismissDialog(WAITING_DIALOG);
	        
	        showDialog(ERROR_DIALOG);
	        
	        m_state++;
	        
	        new Thread(this).start();
	        
    	} else if (m_state == 6) {

    		try {
    			synchronized(this) {
    				wait(ERROR_MESSAGE_DISPLAY_DELAY);
    			}
    		} catch(Exception e) {
    			e.printStackTrace();
    		}
    		
    		m_state++;
    		
    		runOnUiThread(this);
    		
    	} else if (m_state == 7) {
    		
    		dismissDialog(ERROR_DIALOG);
    		
    		m_error = null;

    		finish();
    	}
    }    
    
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);		
	}

	private int m_state=0;
	private AccountFunds m_accountFunds=null;
	private String m_error=null;
	private TextView m_availableBalance=null;
	private TextView m_balance=null;
	private String m_currency="";
}
